<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/6/26
 * Time: 15:16
 */
namespace app\admin\controller;
use think\Db;
class Examine extends Base
{
    public function index()
    {
        if (request()->isAjax()) {
            $params = input('param.');
            if (isset($params['task_type']) && $params['task_type'] != '') {
                $where['task_type'] = $params['task_type'];
            }
            if (isset($params['key']) && $params['key'] != '') {
                $where['task_name'] = ['like', "%" . $params['key'] . "%"];
            }
            $where['a.task_active_status'] = 1;
            if (isset($params['task_active_status']) && $params['task_active_status'] != '') {
                $where['task_active'] = $params['task_active_status'];
            }
            $where['a.task_status'] = array('in', '1,2,3');
            $where['a.task_active'] = ['in','1,2'];
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('task')->alias('a')
                ->join('up_shops us', 'us.s_id = a.task_s_id', 'left')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('task')->alias('a')
                ->join('up_shops us', 'us.s_id = a.task_s_id', 'left')
                ->where($where)
                ->field('a.*,us.s_name,us.s_image')
                ->order('task_time desc')
                ->page($Nowpage, $limits)
                ->select();
            foreach ($lists as $key => &$vo) {
                if ($vo['task_status'] == 1) {
                    $vo['task_status_name'] = '上架';
                } elseif ($vo['task_status'] == 2) {
                    $vo['task_status_name'] = '上架';
                } elseif ($vo['task_status'] == 3) {
                    $vo['task_status_name'] = '上架';
                } elseif ($vo['task_status'] == 4) {
                    $vo['task_status_name'] = '下架';
                }
                if ($vo['task_index'] == 1) {
                    $vo['task_index_name'] = '不推荐';
                } elseif ($vo['task_index'] == 2) {
                    $vo['task_index_name'] = '推荐';
                }
                $vo['wcl'] = ($vo['task_done_num'] / $vo['task_num']) * 100 . '%';
//               $district = Db::table('up_area')->where('district_id',$vo['task_shi'])->value('district');
//               $district1 = Db::table('up_area')->where('district_id',$vo['task_qu'])->value('district');
                if ($vo['task_sheng'] == 0) {
                    $vo['address'] = '全国';
                } else {
                    $city = Db::name('area')
                        ->whereIn('district_id', $vo['task_sheng'])
                        ->column('district');
                    $vo['address'] = implode(',', $city);
                }
//               $vo['address'] = $district.$district1;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    //删除
    public function del(){
        $task_id = input('param.id');
        $task = Db::name('task')->where('task_id',$task_id)
            ->field ('task_s_id,task_num,task_price,task_money,task_done_num,task_status')
            ->find();
        if($task['task_status'] != 4){
            return json(['code'=>100,'msg'=>'只有已下架的才能删除']);
        }else{
            $have = Db::name('task_user')
                ->where('tu_task_id',$task_id)
                ->where('tu_status','in','1,3,4')//如果此时任务已经结束或者下架删除了，拒绝后不会是3，只有进行中的拒绝才是3
                ->find();
            if($have){
                return json(['code'=>100,'msg'=>'此任务还有未审核的或者未提交的，不能删除！']);
            }else{
                $res = Db::name('task')->where('task_id',$task_id)->setField ('task_status',5);
                if($res){
                    return json(['code'=>200,'msg'=>'删除成功']);
                }else{
                    return json(['code'=>100,'msg'=>'删除失败']);
                }
            }
        }
    }

    //下架
    public function lower(){
        $task_id = input('param.id');
        $tasktype = ['error','下载','求赞求粉','问卷','投票','视频','分享'];
        $task = Db::name('task')->where('task_id',$task_id)
            ->field ('task_s_id,task_type,task_num,task_price,task_money,task_done_num,task_status')
            ->find();
        if($task['task_status'] == 3 || $task['task_status'] == 4 || $task['task_status'] == 5){
            return json(['code'=>100,'msg'=>'此任务已结束或已经被下架']);
        }
        // 启动事务
        Db::startTrans();
        try{// 提交事务
            if($task['task_status'] == 1 || $task['task_status'] == 2){
                $back = ($task['task_num'] - $task['task_done_num'])*$task['task_price'];
                if($back > 0){
                    Db::name('shops')
                        ->where('s_id',$task['task_s_id'])
                        ->setInc('s_money',$back);
                    //入商户明细和商户消息
                    allInShopDetail($back,2,$task['task_s_id'],$tasktype[$task['task_type']].'任务下架退还');
                    allInNotice('任务下架退还',$tasktype[$task['task_type']].'任务下架退还，返还'.($back/10).'元！',3,$task['task_s_id']);
                }
            }
            Db::name('task')->where('task_id',$task_id)->update (['task_status'=>4,'task_update_time'=>date('Y-m-d H:i:s')]);
            Db::commit();
            return json(['code'=>200,'msg'=>'下架成功']);
            //=====end===============
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code'=>100,'msg'=>'下架失败']);
        }
    }
    //推荐/不推荐
    public function recommend(){
        $id = input('param.id');
        $where['task_id'] = $id;
        $info = Db::table('up_task')->where($where)->find();
        if($info['task_active_status']==1){
            return json(['code'=>100,'msg'=>'此任务待审核，不能推荐']);
        }
        if($info['task_active_status']==3){
            return json(['code'=>100,'msg'=>'此任务审核未通过，不能推荐']);
        }
        if($info['task_index']==1){
            $data['task_index'] = 2;
            $data['task_index_time'] = date('Y-m-d H:i:s');
        }elseif ($info['task_index']==2){
            $data['task_index'] = 1;
        }
        $res = Db::table('up_task')->where($where)->update($data);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    //领取信息
    public function receive(){
        if(request()->isAjax()){
            $where['a.tu_task_id'] = input('param.id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('task_user')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.tu_user_id','left')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('task_user')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.tu_user_id','left')
                ->where($where)
                ->page($Nowpage,$limits)
                ->field('a.*,uuu.user_username,uuu.user_image,uuu.user_phone')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);

        }
        $this->assign('id',input('param.id'));
        return $this->fetch();
    }
    //任务基本信息
    public function basic(){

        $id = input('param.id');
        $where['task_id'] = $id;
        $info = Db::table('up_task')->where($where)->find();
        if($info['task_type']==1){
            $info['task_type_name'] = '下载';
        }elseif ($info['task_type']==2){
            $info['task_type_name'] = '求赞求粉';
        }elseif ($info['task_type']==3){
            $info['task_type_name'] = '问卷';
        }elseif ($info['task_type']==4){
            $info['task_type_name'] = '投票';
        }elseif ($info['task_type']==5){
            $info['task_type_name'] = '视频';
        }elseif ($info['task_type']==6){
            $info['task_type_name'] = '分享';
        }
//        $district = Db::table('up_area')->where('district_id',$info['task_shi'])->value('district');
//        $district1 = Db::table('up_area')->where('district_id',$info['task_qu'])->value('district');
        $quyu = '';
        if($info['task_sheng'] == 0){
            $quyu = '全国';
        }else{
            $quyu .= Db::table('up_area')
                ->where('district_id',$info['task_sheng'])
                ->value('district');
            if($info['task_shi'] == 0){
                $quyu .= '>全部';
            }else{
                $quyu .= '>' . Db::table('up_area')
                        ->where('district_id',$info['task_shi'])
                        ->value('district');
                if($info['task_sheng'] != 0 && $info['task_shi'] != 0){
                    $qus = Db::table('up_area')
                        ->where('district_id','in',$info['task_qu'])
                        ->column('district');
                    $quyu .= '>' . implode($qus,' ');
                }else{
                    $quyu .= '>全部';
                }
            }
        }
        $info['address'] = $quyu;
        $info['wcl'] = ($info['task_done_num']/$info['task_num'])*100 . '%';;
        $this->assign('info',$info);
        return $this->fetch();
    }

    //审核
    public function verify(){
        if(request()->isPost()){
            $id = input('param.id');
            $today = date('Y-m-d H:i:s');
            $task = Db::table('up_task')->where('task_id',$id)->field('task_price,task_s_id,task_type,task_endtime,task_status')->find();
            if($task['task_endtime'] <= $today){
                return json(['status'=>100,'msg'=>'此任务已过了结束时间，不能审核，请下架！']);
            }
            if($task['task_status'] != 1){
                return json(['status'=>100,'msg'=>'此任务已不能审核了！']);
            }

            $data['task_active_status'] = input('param.s_status');
            $data['task_active_reason'] = input('param.s_reason');
            $res = Db::table('up_task')->where('task_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('param.id');
        $where['task_id'] = $id;
        $info = Db::table('up_task')->where($where)->find();
        if($info['task_type']==1){
            $info['task_type_name'] = '下载';
        }elseif ($info['task_type']==2){
            $info['task_type_name'] = '求赞求粉';
        }elseif ($info['task_type']==3){
            $info['task_type_name'] = '问卷';
        }elseif ($info['task_type']==4){
            $info['task_type_name'] = '投票';
        }elseif ($info['task_type']==5){
            $info['task_type_name'] = '视频';
        }elseif ($info['task_type']==6){
            $info['task_type_name'] = '分享';
        }
//        $district = Db::table('up_area')->where('district_id',$info['task_shi'])->value('district');
//        $district1 = Db::table('up_area')->where('district_id',$info['task_qu'])->value('district');
        $quyu = '';
        if($info['task_sheng'] == 0){
            $quyu = '全国';
        }else{
            $quyu .= Db::table('up_area')
                ->where('district_id',$info['task_sheng'])
                ->value('district');
            if($info['task_shi'] == 0){
                $quyu .= '>全部';
            }else{
                $quyu .= '>' . Db::table('up_area')
                        ->where('district_id',$info['task_shi'])
                        ->value('district');
                if($info['task_sheng'] != 0 && $info['task_shi'] != 0){
                    $qus = Db::table('up_area')
                        ->where('district_id','in',$info['task_qu'])
                        ->column('district');
                    $quyu .= '>' . implode($qus,' ');
                }else{
                    $quyu .= '>全部';
                }
            }
        }
        $info['address'] = $quyu;
        $info['wcl'] = ($info['task_done_num']/$info['task_num'])*100 . '%';;
        $this->assign('info',$info);
        return $this->fetch();
    }
}